<?php

namespace Budget\BudgetBundle\Model\Charts;

use Budget\BudgetBundle\Helper\DateHelper;
use Budget\BudgetBundle\Entity\Family;

class ChartExpenses extends ChartAbstract {
       
    public function getToDataJson($expenses, $dateTo, $dateFrom)
    {       
        $categoriesID = $months = $response = $monthsName = array();
        if ($dateTo >= $dateFrom) {
            $arrayDateFrom = explode('-', $dateFrom);
            $i  = 0;
            $mm = $arrayDateFrom[1];
            $yy = $arrayDateFrom[0];
            while (true) {
                $months[$i]   = $yy.'-'.$mm;
                //$response[$i] = array(DateHelper::getMonthName($mm), 0);
                $monthsName[$i] = DateHelper::getMonthName($mm);
                $i++;
                if ($yy.'-'.$mm == $dateTo) {
                    break; 
                } else {
                    if ($mm == 12) {
                       $mm  = 1;
                       $yy += 1;
                    } else {
                       $mm += 1;
                    }
                    if ($mm < 10) {
                        $mm = '0'.$mm;
                    }
                } 
            }
        }
        
        $count = count($months);
        foreach ($expenses AS $expense) {  
            $month = $expense['date_created']->format(DateHelper::FORMAT_YEAR_MONTH); 
            for($i=0; $i<$count; $i++) {
                if($months[$i] == $month) {
                    break;
                }
            }
            
            $key = array_search($expense['category']['id'], $categoriesID);
            if(!$key) {
                $categoriesID[] = $expense['category']['id'];
                $key = count($categoriesID) - 1;
                for($j=0; $j<$count; $j++) {
                    $response[$key][$j] = 0;     
                }      
            } 
            $response[$key][$i] = $response[$key][$i] + $expense['price']; 
            
            //$response[$i][1] = $response[$i][1] + $expense['price'];
        }

        return array($monthsName, $response);
    }
    
    public function getDataForFamilyToArray(Family $family, $page, $limit, $sidx, $sord, $where)
    {
        if (!$sidx) {
            $sidx = 1;
        }
   
        if (!$where) {
            $date = date(DateHelper::FORMAT_YEAR_MONTH);
            $whereSQL = " AND e.date >= '".$date."-01 ".DateHelper::DAY_START."' AND e.date <= '".$date."-31 ".DateHelper::DAY_END."'";
        } else { 
            //$whereSQL = $where['where'];     
            $whereSQL = '';        
        }
                   
        $qb = $this->getEntityManager()->getRepository('BudgetBudgetBundle:Expense')->createQueryBuilder('e')
                   ->select('e', 'c')
                   ->leftJoin('e.category', 'c')
                   ->where('c.family = :family')
                   ->andWhere('e.date_deleted IS NULL')
                   ->andWhere('c.date_deleted IS NULL'.$whereSQL)
                   ->addOrderBy($sidx , $sord)
                   ->setParameter('family' , $family->getId());
     
        if ($where) {
            for ($i=0; $i<count($where['parameters']); $i++) {
                $qb->andWhere($where['where'][$i]);
                $qb->setParameter($where['parameters'][$i]['name'], $where['parameters'][$i]['val']);
            }
        }
  
        return  $qb->getQuery()->getArrayResult();  
    }
}